package bean;

import bean.dao.IUserDao;
import bean.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@Slf4j
public class app {

    /**
     * 获取spring的IOC核心容器,并根据ID获取对象
     * @param args
     */
    public static void main(String[] args) {
        // UserServiceImpl userService = new UserServiceImpl();
        // userService.selectAll();

        // 1. 获取核心容器对象, 配置中的对象会立刻初始化
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 输出:  UserServiceImpl 已创建成功
        System.out.println("遍历开始");
        for (int i = 0; i < 3; i++) {
            // 2. 根据Bean Id获取bean对象
            // 2.1 方式一: 强转
            IUserService userService = (IUserService) ac.getBean("userService");
            // 2.2 方式二: 指定返回类型
            IUserDao userDao = ac.getBean("userDao", IUserDao.class);
            // 输出对象都为单例
            System.out.println(userService);
            System.out.println(userDao);
        }

        // 别名也是单例和之前的结果一致
        IUserService userService = (IUserService) ac.getBean("uService");
        System.out.println("alias test ------> " + userService); // 单例




    }
}
